<template>
    <div id="analyze">
        <!--工具栏目-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0;">
            <el-form :inline="true" :model="Andata">
                <el-form-item>
                    <el-input type="number" min="0" v-model="Andata.id" placeholder="请输入要搜索的ID"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-input v-model="Andata.title" placeholder="请输入要搜索的标题"></el-input>
                </el-form-item>
                <el-select v-model="Andata.type" clearable placeholder="请选择">
                    <el-option label="大咖论市" :value="1"></el-option>
                    <el-option label="股民选股" :value="2"></el-option>
                    <el-option label="投资策略" :value="3"></el-option>
                    <el-option label="金融快讯" :value="4"></el-option>
                </el-select>
                <el-form-item>
                    <el-button type="primary" @click="getList">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>

        <!--TAble-->
        <el-table :data="AnalList" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
                  style="width: 100%;">
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column prop="id" label="ID" width="80"></el-table-column>
            <el-table-column prop="title" label="标题"></el-table-column>
            <el-table-column prop="mainImg" label='图片'>
                <template slot-scope='scope'>
                    <img v-if="scope.row.mainImg && scope.row.mainImg.path" :src='scope.row.mainImg.path'
                         class="mainImg">
                    <span v-else>暂未有图片</span>
                </template>
            </el-table-column>
            <el-table-column prop="user.username" dsds="user.username" label="昵称"></el-table-column>
            <el-table-column label="类型">
                <template slot-scope="scope">
                    <p v-if="scope.row.type === 1">大咖论市</p>
                    <p v-if="scope.row.type === 2">股民选股</p>
                    <p v-if="scope.row.type === 3">投资策略</p>
                    <p v-if="scope.row.type === 4">金融快讯</p>
                </template>
            </el-table-column>
            <el-table-column label="操作" width="220">
                <template slot-scope="scope">
                    <el-button size="small"  @click="handleConnect(scope.$index, scope.row)">打开连接</el-button>
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--分页-->
        <el-col :span="24" class="toolbar">
            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="size"
                           :total="total" style="float:right;"></el-pagination>
        </el-col>

        <!--编辑-->
        <el-dialog title="编辑" v-model="editFormVisible" :close-on-click-modal="false" size="large" top="5%"
                   :before-close="abolish">
            <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
                <el-form-item label="标题" prop="title">
                    <el-input v-model="editForm.title" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="类型选择">
                    <el-select v-model="editForm.type">
                        <el-option label="股民选股" :value="2"></el-option>
                        <el-option label="投资策略" :value="3"></el-option>
                        <el-option label="金融快讯" :value="4"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="内容" prop="body">
                    <UEditor @ready="editorReadyEdit"></UEditor>
                    <!--<el-input v-model="editForm.body"></el-input>-->
                </el-form-item>
                <el-form-item label="展示图片" prop="mainImg">
                    <el-upload class="avatar-uploader" :action="$store.state.imagesUrl + 'api/common/file/upload/image'"
                               :show-file-list="false" :on-success="handleAvatarSuccess1"
                               :before-upload="beforeAvatarUpload" :headers="MyHeaders">
                        <img v-if="imageUrl" :src="imageUrl" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="abolish">取消</el-button>
                <el-button type="primary" @click.native="editSubmit('editForm')" :loading="editLoading">提交</el-button>
            </div>
        </el-dialog>
        <!--添加-->
        <el-dialog title="添加分析" v-model="addFormVisible" :close-on-click-modal="false" size="large" top="5%">
            <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
                <el-form-item label="标题" prop="title">
                    <el-input v-model="addForm.title" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="类型选择">
                    <el-select v-model="addForm.type">
                        <el-option label="股民选股" :value="2"></el-option>
                        <el-option label="投资策略" :value="3"></el-option>
                        <el-option label="金融快讯" :value="4"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="内容">
                    <UEditor @ready="editorReady"></UEditor>
                </el-form-item>
                <el-form-item label="展示图片" prop="mainImg">
                    <el-upload class="avatar-uploader" :action="$store.state.imagesUrl + 'api/common/file/upload/image'"
                               :show-file-list="false" :on-success="handleAvatarSuccess"
                               :before-upload="beforeAvatarUpload" :headers="MyHeaders">
                        <img v-if="imageUrl" :src="imageUrl" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit('addForm')" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>


    </div>
</template>
<script>
    import util from '../../common/js/util';
    import {analysisList, analysisDel, analysisNew, analysisEdit} from '../../api/api';
    import UEditor from '../../components/ueditor/index';

    export default {
        data: function () {
            return {
                AnalList: [],
                total: 0,
                page: 1,
                size: 15,
                Andata: {
                    id: null,
                    title: ''
                },
                listLoading: false,
                sels: [],
                /*编辑界面*/
                editFormVisible: false,//编辑界面是否显示
                editLoading: false,
                ueEdit: null,
                editFormRules: {
                    title: {required: true, message: '请输入标题', trigger: 'blur'}
                },
                //编辑界面数据
                editForm: {
                    id: null,
                    title: '',
                    mainImg: {
                        path: ''
                    }
                },
                /*新增界面*/
                addFormVisible: false,
                addLoading: false,
                addFormRules: {
                    title: {required: true, message: '请输入标题', trigger: 'blur'}
                },
                MyHeaders: {'X-Authentication': JSON.parse(sessionStorage.getItem('user'))},
                /*新增界面数据*/
                imageUrl: '',
                ueAdd: null,
                addForm: {
                    title: '',
                    mainImg: {
                        path: ''
                    },
                    type: 2
                },
            }
        },
        methods: {
            editorReady(editorInstance) {
                this.ueAdd = editorInstance;
                editorInstance.setContent('');
                editorInstance.addListener('contentChange', () => {
                    console.log('内容发生了变化：', editorInstance.getContent());
                });
            },
            editorReadyEdit(editorInstance) {
                this.ueEdit = editorInstance;
                editorInstance.setContent('');
                editorInstance.addListener('contentChange', () => {
                    console.log('内容发生了变化：', editorInstance.getContent());
                });
            },
            selsChange: function (sels) {
                this.sels = sels;
            },
            handleCurrentChange(val) {
                this.page = val;
                this.getList();
            },
            handleConnect(index,row){
                window.open(this.$store.state.imagesUrl+'wap/app/analysis/info/?id='+row.id);
            },
            /*获取*/
            getList() {
                let para = {
                    page: this.page,
                    size: this.size,
                    data: this.Andata
                };
                this.listLoading = true;
                analysisList(para).then((res) => {
                    console.log(res.data)
                    this.listLoading = false;
                    let {code, data} = res;
                    if (code !== 20000) {
                        this.$message({message: res.message, type: 'error'});
                    } else {
                        this.AnalList = data.content;
                        this.total = data.totalElements;
                    }
                });
            },
            /*删除*/
            handleDel(index, row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    let para = {id: row.id};
                    let id = row.id;
                    analysisDel(id, para).then((res) => {
                        this.listLoading = false;
                        this.$message({
                            message: res.message,
                            type: 'success'
                        });
                        this.getList();
                    });
                }).catch(() => {
                });
            },
            /*显示编辑界面*/
            handleEdit(index, row) {
                this.editFormVisible = true;
                this.editForm = Object.assign({}, row);
                if (row.mainImg === null) {
                    this.imageUrl = '';
                    this.editForm.mainImg = {
                        path: ''
                    }
                } else {
                    this.imageUrl = row.mainImg.path;
                }
                var time = setInterval(() => {
                    if (this.ueEdit !== null) {
                        this.ueEdit.setContent(row.body);
                        clearInterval(time);
                    }
                }, 100)
            },
            /*取消编辑界面*/
            abolish() {
                this.editFormVisible = false;
                this.getList();
            },
            /*显示新增界面*/
            handleAdd() {
                this.addFormVisible = true;
                this.addForm = {
                    title: '',
                    mainImg: {path: ''},
                    type: 1
                };
                this.imageUrl = '';
                if (this.ueAdd !== null) {
                    this.ueAdd.setContent('');
                }
            },
            /*上传*/
            handleAvatarSuccess(res, file) {
                this.imageUrl = file.response.data.pathFull;
                this.addForm.mainImg.path = file.response.data.path;
            },
            handleAvatarSuccess1(res, file) {
                this.imageUrl = file.response.data.pathFull;
                this.editForm.mainImg.path = file.response.data.path;
            },
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg' || 'image/png';
                const isLt2M = file.size / 1024 / 1024 < 2;
                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG/png 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            },
            /*新增*/
            addSubmit(a) {
                this.$refs[a].validate((valid) => {
                    if (valid) {
                        if (this.ueAdd.getContent() === '') {
                            this.$message({message: '请填写内容', type: 'error'});
                            return;
                        }
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.addLoading = true;
                            let para = this.addForm;
                            para.body = this.ueAdd.getContent();
                            if(typeof para.mainImg === 'string'){
                                para.mainImg={};
                            }
                            analysisNew(para).then((res) => {
                                this.addLoading = false;
                                this.$message({
                                    message: res.message,
                                    type: 'success'
                                });
                                this.addFormVisible = false;
                                this.getList();
                            });
                        });
                    }
                })

            },
            /*编辑*/
            editSubmit(a) {
                this.$refs[a].validate((valid) => {
                    if (valid) {
                        if (this.ueEdit.getContent() === '') {
                            this.$message({message: '请填写内容', type: 'error'});
                            return;
                        }
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.editFormVisible = true;
                            let para = this.editForm;
                            para.body = this.ueEdit.getContent();
                            if(typeof para.mainImg === 'string'){
                                para.mainImg={};
                            }
                            analysisEdit(para).then((res) => {
                                this.editLoading = false;
                                this.$message({
                                    message: res.message,
                                    type: 'success'
                                });
                                this.editFormVisible = false;
                                this.getList();
                            });
                        });
                    }
                })
            },

        },
        mounted() {
            this.getList();
        },
        components: {
            UEditor
        },
    }
</script>
<style type="text/scss" lang="scss" scoped>
    #analyze {
        .mainImg {
            width: auto;
            height: 20px;
            vertical-align: middle;
        }
        .avatar-uploader {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
            width: 182px;
            &:hover {
                border-color: #20a0ff;
            }
        }
        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
        }
        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }
    }
</style>